/*

Copyright (C) 2000 - 2008 Christian Kreibich <christian@whoop.org>.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
#ifndef __nd_macros_h
#define __nd_macros_h

#include <stdlib.h>

/**
 * return_if_no_current_trace - current trace safety check.
 * @tr: variable in which current trace is stored.
 *
 * The macro checks whether there is currently a trace being
 * edited. If that is the case, this trace is stored in @tr,
 * otherwise the macro returns from the current function.
 */
#define return_if_no_current_trace(tr) \
do { tr = nd_trace_registry_get_current(); D_ASSERT_PTR(tr); if (!tr) return; } while (0)

/**
 * return_val_if_no_current_trace - version of return_if_no_current_trace with return value.
 * @tr: variable in which current trace is stored.
 * @val: return value when there is no current trace.
 *
 * This is the same macro as return_if_no_current_trace(), but
 * it returns @val is there is no current trace.
 */
#define return_val_if_no_current_trace(tr, val) \
do { tr = nd_trace_registry_get_current(); D_ASSERT_PTR(tr); if (!tr) return (val); } while (0)


/**
 * ND_GTK_GET_DATA - g_object_get_data wrapper
 * @res: result object
 * @objs: object list to query
 * @key: key name of data to look up
 * 
 * The macro is a wrapper for g_object_get_data()
 * that also asserts the existance of the pointer on return,
 * pointing out where in the program an invalid key was
 * used.
 */
#define ND_GTK_GET_DATA(res, obj, key) \
do { \
  res = g_object_get_data (obj, key); \
  D_ASSERT_PTR(res); \
} while (0)


/**
 * ND_GTK_GET_DATA_TYPE - ND_GTK_GET_DATA wrapper.
 * @type: GTK type-cast of the resulting object (i.e. GTK_WINDOW)
 * @res: result object
 * @objs: object list to query
 * @key: key name of data to look up
 * 
 * The macro is a wrapper for the ND_GTK_GET_DATA macro to allow
 * quick casting to various GTK types.
 */
#define ND_GTK_GET_DATA_TYPE(type, res, obj, key) \
do { \
  GObject *o = NULL; \
  ND_GTK_GET_DATA(o, obj, key); \
  res = type( o ); \
} while (0)


/**
 * ND_GTK_GET - gtk_builder_get_object wrapper
 * @res: result object
 * @objs: object list to query
 * @key: key name of data to look up
 * 
 * The macro is a wrapper for nd_gtk_builder_get_object()
 * that also asserts the existance of the pointer on return,
 * pointing out where in the program an invalid key was
 * used.
 */
#define ND_GTK_GET(res, builder, key) \
do { \
  res = gtk_builder_get_object (builder, key); \
  D_ASSERT_PTR(res); \
} while (0)


/**
 * ND_GTK_GET_TYPE - ND_GTK_GET wrapper.
 * @type: GTK type-cast of the resulting object (i.e. GTK_WINDOW)
 * @res: result object
 * @objs: object list to query
 * @key: key name of data to look up
 * 
 * The macro is a wrapper for the ND_GTK_GET macro to allow
 * quick casting to various GTK types.
 */
#define ND_GTK_GET_TYPE(type, res, builder, key) \
do { \
  GObject *o = NULL; \
  ND_GTK_GET(o, builder, key); \
  res = type( o ); \
} while (0)


/**
 * ND_GTK_GET_WIDGET - ND_GTK_GET wrapper.
 * @res: result object
 * @objs: object list to query
 * @key: key name of data to look up
 * 
 * The macro is a wrapper for the ND_GTK_GET macro to allow
 * quick casting to the widget GTK type.
 */
#define ND_GTK_GET_WIDGET(res, objs, key) \
do { \
  GObject *o = NULL; \
  ND_GTK_GET(o, objs, key); \
  res = GTK_WIDGET( o ); \
} while (0)

#endif /* __nd_macros_h */
